
import {get,post} from '../../http'
const SELCOMPANY = 'SELCOMPANY';
const ADDCOMPANY = 'ADDCOMPANY'
const SELUSER = 'SELUSER';
const ADDPWD = 'ADDPWD'

const EMPLOYEE = 'EMPLOYEE'
const SELQX = 'SELQX'
const ADDEMPLOYEE = 'ADDEMPLOYEE'
const DELEMPLOYEE = 'DELEMPLOYEE'
const EDITEMPLOYEE = 'EDITEMPLOYEE'

const initState={
     companys:{},
     companyState:false,
     pwdState:false,
     uesr:{},
     employData:{},
     qxData:{},
     AddEmploy : false,
     delEmploy:false,
     editEmploy:false
}
// reducer1
export function company(state=initState, action){
	switch(action.type){
		case SELCOMPANY:
            return Object.assign({},state,{companys:action.res,companyState:false})
        case ADDCOMPANY:
            return Object.assign({},state,{companyState:action.res})
        case SELUSER:
            return Object.assign({},state,{uesr:action.res,pwdState:false})
        case ADDPWD:
            return Object.assign({},state,{pwdState:action.res})
        case EMPLOYEE:
            return Object.assign({},state,{employData:action.res,AddEmploy:false,delEmploy:false})
        case SELQX:
            return Object.assign({},state,{qxData:action.res})
        case ADDEMPLOYEE:
            return Object.assign({},state,{AddEmploy:action.res})
        case DELEMPLOYEE:
            return Object.assign({},state,{delEmploy:action.res})
        case EDITEMPLOYEE:
            return Object.assign({},state,{editEmploy:action.res})
		default:
			return state
	}
} 
function editmployeeSuccess(res){
	return {type:EDITEMPLOYEE,res:res}
}

function delemployeeSuccess(res){
	return {type:DELEMPLOYEE,res:res}
}


function employeeSuccess(res){
	return {type:EMPLOYEE,res:res}
}

function AddemployeeSuccess(res){
	return {type:ADDEMPLOYEE,res:res}
}

function companysSuccess(res){
	return {type:SELCOMPANY,res:res}
}

function addCompanysSuccess(res){
	return {type:ADDCOMPANY,res:res}
}
function UserSuccess(res){
    console.log(res)
	return {type:SELUSER,res:res}
}
function qxSuccess(res){
    console.log(res)
	return {type:SELQX,res:res}
}

function addPwdSuccess(res){
	return {type:ADDPWD,res:res}
}
export function editUser (userId,account,phoneNum,roleId){
    let data ={
        account,userId,phoneNum,roleId
    }
	return dispatch=>{
		post('user/editUser.do',data)
			.then(res=>{
				dispatch(editmployeeSuccess(res.SUCCESS));
			})		
	}
	
}

export function addEmploy(account,password,phoneNum,roleId){
    let data ={
        account,password,phoneNum,roleId
    }
	return dispatch=>{
		post('user/addUser.do',data)
			.then(res=>{
				dispatch(AddemployeeSuccess(res.SUCCESS));
			})		
	}
	
}

export function delEmploy(userId){
    let data ={
        userId,
    }
	return dispatch=>{
		post('user/deleteUser.do',data)
			.then(res=>{
				dispatch(delemployeeSuccess(res.SUCCESS));
			})		
	}
	
}



export function queryRole(){
	return dispatch=>{
		post('config/queryRole.do',{})
			.then(res=>{
				dispatch(qxSuccess(res.roleList));
			})		
	}
	
}

export function queryAllCompanyUser(page,limit){
    let data ={
        page,limit
    }
	return dispatch=>{
		post('user/queryAllCompanyUser.do',data)
			.then(res=>{
				dispatch(employeeSuccess(res.data));
			})		
	}
	
}


export function UserMenu(){
    let data ={
    }
	return dispatch=>{
		post('user/queryUser.do',data)
			.then(res=>{
				dispatch(UserSuccess(res));
			})		
	}
	
}

export function addPwd (oldPassword,newPassword){
    let data ={
        oldPassword,newPassword
    }
	return dispatch=>{
		post('user/editPassword.do',data)
			.then(res=>{
				dispatch(addPwdSuccess(res.state));
			})		
	}
	
}

export function companyMenu (){
    let data ={
        
    }
	return dispatch=>{
		post('company/queryCompany.do',data)
			.then(res=>{
				console.log(res)
				dispatch(companysSuccess(res));
			})		
	}
	
}

export function AddCompany (address,phone,note){
    let data ={
        address,phone,note
    }
	return dispatch=>{
		post('company/editCompany.do',data)
			.then(res=>{
				console.log(res)
				dispatch(addCompanysSuccess(res.SUCCESS));
			})		
	}
	
}




